import { _decorator, Component, EventKeyboard, input, Input, KeyCode } from 'cc';
import { GamePlayer } from './GamePlayer';
import { DIRECTION_ENUM, EAT_STATUS_ENUM, GAME_MODE_ENUM, PARAME_NAME_ENUM, SOCKET_TYPE } from '../../Enums';
import { AttackManager } from './AttackManager';
import DataManager from '../Runtime/DataManager';
import { sendSocket } from '../service/socket';
import { userStore } from '../service/user';
import EventManager from '../Runtime/EventManager';
const { ccclass } = _decorator;
// 字母控制
@ccclass('AlphabetControl')
export class AlphabetControl extends Component {
    gamePlayer: GamePlayer
    playManager
    attackPlayer
    status: string
    init() {
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this); //键盘按下
        this.gamePlayer = this.node.getComponent(GamePlayer)
        this.attackPlayer = this.node.getComponent(GamePlayer).getComponent(AttackManager)
        setTimeout(() => {
            this.playManager = this.node.getComponent(GamePlayer).playManager
            this.attackPlayer = this.node.getComponent(GamePlayer).attackPlayer
        }, 10)
        EventManager.Instance.on('msg', this.websocketMessage, this);
    }
    onDestroy() {
        input.off(Input.EventType.KEY_UP, this.onKeyUp);
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown)
        EventManager.Instance.off("msg", this.websocketMessage)
    }
    async websocketMessage(data) {
        if (data.roomId != DataManager.Instance.room.id) return
        if (data.type == SOCKET_TYPE.KEYDOWN || data.type == SOCKET_TYPE.KEYUP) {
            if (this.node.name != data.content.name) return
            if (data.type == SOCKET_TYPE.KEYDOWN) {
                this.keyDown(data.content.event)
            }
            if (data.type == SOCKET_TYPE.KEYUP) {
                this.keyUp(data.content.event)
            }
        }

    }
    // 按下键盘
    onKeyDown(event: EventKeyboard) {
        if (DataManager.Instance.gameMode == GAME_MODE_ENUM.ONLINE) {
            // 房主用键盘
            if (userStore()?.info?.id == DataManager.Instance.room.memberId) return
            switch (event.keyCode) {
                case KeyCode.KEY_W:
                case KeyCode.KEY_A:
                case KeyCode.KEY_S:
                case KeyCode.KEY_D:
                case KeyCode.CTRL_LEFT:
                    sendSocket(SOCKET_TYPE.KEYDOWN, {
                        name: this.node.name,
                        event
                    })
                    break;
            }
        } else {
            this.keyDown(event)
        }
    }
    keyDown(event: EventKeyboard, direction: DIRECTION_ENUM = null) {
        switch (event.keyCode) {
            case KeyCode.KEY_W:
                this.gamePlayer.setStatus("top")
                break;
            case KeyCode.KEY_A:

                this.gamePlayer.setStatus("left")
                break;
            case KeyCode.KEY_S:

                this.gamePlayer.setStatus("bottom")
                break;
            case KeyCode.KEY_D:
                this.gamePlayer.setStatus("right")
                break;
            case KeyCode.CTRL_LEFT:
                if (DataManager.Instance.gameMode != GAME_MODE_ENUM.SINGLE) {
                    this.playAttack()
                }
                break;
            case KeyCode.SPACE:
                if (DataManager.Instance.gameMode == GAME_MODE_ENUM.SINGLE) {
                    this.playAttack()
                }
                break;
        }
    }
    playAttack() {
        if(!this.playManager) return
        let { direction, eatFood } = this.playManager
        // 进行吃东西动画的时候禁止点击
        if (!this.playManager.fsm.paramsName.includes('idle')) return
        if (eatFood == EAT_STATUS_ENUM.EMPTY) {
            this.attackPlayer.launchAttack(EAT_STATUS_ENUM.EMPTY, direction)
            if (direction == DIRECTION_ENUM.TOP) {
                this.gamePlayer.state = PARAME_NAME_ENUM.TOPPROCESS
            } else if (direction == DIRECTION_ENUM.LEFT) {
                this.gamePlayer.state = PARAME_NAME_ENUM.LEFTPROCESS
            } else if (direction == DIRECTION_ENUM.BOTTOM) {
                this.gamePlayer.state = PARAME_NAME_ENUM.BOTTOMPROCESS
            } else if (direction == DIRECTION_ENUM.RIGHT) {
                this.gamePlayer.state = PARAME_NAME_ENUM.RIGHTPROCESS
            }
            DataManager.Instance.audioMgr.playOneShot(DataManager.Instance.audio.find(item => item.name == '1229'))
            return
        } else {
            // 如果面前有障碍物,就不能吐
            let list = this.playManager.obstacle.filter(item => item.active == true && !item.name.includes("enemy"))
            if (list.length > 0) return
            this.gamePlayer.eatFood = EAT_STATUS_ENUM.EMPTY
            if (direction == DIRECTION_ENUM.TOP) {
                this.gamePlayer.state = PARAME_NAME_ENUM.TOPPROCESS
            } else if (direction == DIRECTION_ENUM.LEFT) {
                this.gamePlayer.state = PARAME_NAME_ENUM.LEFTPROCESS
            } else if (direction == DIRECTION_ENUM.BOTTOM) {
                this.gamePlayer.state = PARAME_NAME_ENUM.BOTTOMPROCESS
            } else if (direction == DIRECTION_ENUM.RIGHT) {
                this.gamePlayer.state = PARAME_NAME_ENUM.RIGHTPROCESS
            }
            // 延时，配合动画
            setTimeout(() => {
                DataManager.Instance.audioMgr.playOneShot(DataManager.Instance.audio.find(item => item.name == '1233'))
                this.attackPlayer.launchAttack(eatFood, direction)
            }, 300)

        }
    }
    onKeyUp(event: EventKeyboard) {
        if (DataManager.Instance.gameMode == GAME_MODE_ENUM.ONLINE) {
            if (userStore()?.info?.id == DataManager.Instance.room.memberId) return
            switch (event.keyCode) {
                case KeyCode.KEY_W:
                case KeyCode.KEY_A:
                case KeyCode.KEY_S:
                case KeyCode.KEY_D:
                    sendSocket(SOCKET_TYPE.KEYUP, {
                        name: this.node.name,
                        event
                    })
                    break;
            }
        } else {
            this.keyUp(event)
        }
    }
    keyUp(event) {
        let keyCode
        switch (event.keyCode) {
            case KeyCode.KEY_W:
                keyCode = "top"
                break;
            case KeyCode.KEY_A:
                keyCode = "left"
                break;
            case KeyCode.KEY_S:
                keyCode = "bottom"
                break;
            case KeyCode.KEY_D:
                keyCode = "right"
                break;
        }
        if (keyCode == this.gamePlayer.next_status) {
            this.gamePlayer.next_status = ""
        }
        if (keyCode == this.gamePlayer.status) {
            this.gamePlayer.setStatus("")
            if (!this.playManager.forcedWalking) {
                this.gamePlayer.walkingLogic(keyCode)
            }
        }
    }
}


